package com.zch.util.file.txt;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
/**
 * 自定义写日志方法
 *
 * @author zchcpy
 * @date 180913
 */
@Slf4j
public class OutPutTxt {
  private OutPutTxt() {
  }
  public static final char KEY_CHAR_TAB = '\t';
  public static final char KEY_CHAR_ENTER = '\n';
  public static final String PATH_BASE_WIN = "E:" + File.separator + "LogMessage";
  /**
   * /usr/singbon/logs/
   */
  public static final String PATH_BASE_LINUX = File.separator + "usr" + File.separator + "singbon" + File.separator + "logs" + File.separator;
  public static final String PATH_BASE_MAC = File.separator + "Document" + File.separator + "singbon" + File.separator + "logs" + File.separator;
  /**
   * 日志路径 相对路径
   */
  public static final String PATH_SYNC_ERR_CONSUME = File.separator + "middle-db-transfer" + File.separator + "Sync1ConsumeRecordsTask" + File.separator;
  public static final String PATH_SYNC_ERR_CUSTOMERS = File.separator + "middle-db-transfer" + File.separator + "Sync2CustomerTask" + File.separator;
  public static final String PATH_SYNC_ERR_CARD_RECORD = File.separator + "middle-db-transfer" + File.separator + "Sync3ChargeRecordsTask" + File.separator;
  /**
   * 定时任务实现类 过程中的日志
   */
  public static final String NAME_SYNC_ERR_CONSUME = "sync-consumes-err.log";
  public static final String NAME_SYNC_ERR_CARD_RECORD = "sync-card-records-cycle.log";
  public static final String NAME_SYNC_ERR_CUSTOMERS = "sync-customers-err.log";
  /**
   * 定时任务循环执行过程中的日志
   */
  public static final String NAME_SYNC_CYCLE_CONSUME = "sync-consumes-cycle.log";
  public static final String NAME_SYNC_CYCLE_CUSTOMERS = "sync-customers-cycle.log";
  public static final String NAME_SYNC_CYCLE_CARD_RECORD = "sync-card-records-cycle.log";
  /**
   * 需要特殊保存的记录
   */
  public static final String NAME_SYNC_ERR_NEED_CUSTOMER = "sync-need-customer-err.log";
  /**
   *
   */
  public static final String WINDOWS = "windows";
  public static final String LINUX = "linux";
  public static final String MAC = "mac";
  public static final Boolean IS_APPEND = true;
  /**
   * writeLogToFile isAppend 方法实现
   */
  private static void writeToFileImpl(String path, String name, String content, boolean isAppend, Charset charset) {
    try {
      content = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATETIME_MS_PATTERN) + KEY_CHAR_TAB + content;
      // name = DateUtil.today() + "-" + name
      path = path + File.separator + name;
    }
    catch (Exception e) {
      log.warn("文件写入失败,路径错误:path:{},name:{}", path, name);
      return;
    }
    //(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, isAppend), StandardCharsets.UTF_8)))
    try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, isAppend), charset))) {
      out.write(content);
      out.flush();
    }
    catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }
  /**
   * 写日志 logRightNow
   */
  public static void writeToFile(String path, String name, String content, boolean isAppend, Charset charset) {
    if (StringUtils.isEmpty(getLogRootPath())) {
      log.warn("日志根路径获取失败:path:{},name:{},content:{},", path, name, content);
      return;
    }
    path = getLogRootPath() + path;
    File dic = new File(path);
    if (!dic.exists()) {
      // 目录不存在的情况下，创建目录。
      boolean mkResult = dic.mkdirs();
      if (mkResult) {
        writeToFileImpl(path, name, content, isAppend, charset);
      }
    }
    else {
      writeToFileImpl(path, name, content, isAppend, charset);
    }
  }
  /**
   * 获取系统名称
   */
  public static String getOsNameLowerCase() {
    return System.getProperties().getProperty("os.name").toLowerCase();
  }
  /**
   * 获取系统根路径
   */
  public static String getLogRootPath() {
    String osName = getOsNameLowerCase();
    if (osName.contains(WINDOWS)) {
      return PATH_BASE_WIN;
    }
    else if (osName.contains(LINUX)) {
      return PATH_BASE_LINUX;
    }
    else if (osName.contains(MAC)) {
      return PATH_BASE_MAC;
    }
    else {
      return "";
    }
  }
}
